package com.botarena.client.bot;

import java.util.logging.Logger;

import com.botarena.client.SiteTokens;
import com.botarena.client.UIHelper;
import com.botarena.client.bot.remote.BotAccess;
import com.botarena.client.bot.remote.BotAccessAsync;
import com.botarena.client.ui.StringHelper;
import com.botarena.shared.BotHomeInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class BotHomepage extends VerticalPanel {
	
	private static final String SHOW_FIGHTS_BUTTON_LABEL = "Show fights";
	private static final String SHOW_BUTTONS_WIDTH = "100px";
	
	private static final Logger log = Logger.getLogger(BotHomepage.class.getName());

	
	private String botKey;
	private BotHomeInfo botHome;
	
	private Button showFights;
	
	public BotHomepage(String botKey) {
		this.botKey = botKey;
		
		initShowFightsButton();
		getData();
	}

	private void getData() {
		BotAccessAsync botService = GWT.create(BotAccess.class);
		botService.getHomeInfo(botKey, new AsyncCallback<BotHomeInfo>() {

			@Override
			public void onFailure(Throwable caught) {
				clear();
				add(new Label("Sorry, unable to retrieve data..."));
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(BotHomeInfo result) {
				botHome = result;
				refresh();
			}
		});
		clear();
		add(UIHelper.createBigLoader("Retrieving data..."));
	}

	protected void refresh() {
		clear();
		
		Label leftTitle = new Label("General info:");
		leftTitle.setStyleName("title5");
		
		FlexTable leftFormTable = new FlexTable();
		leftFormTable.addStyleName("botHomeFormTable");
		
		leftFormTable.setWidget(0, 0, new Label("Name:"));
		leftFormTable.setWidget(1, 0, new Label("Author:"));
		leftFormTable.setWidget(2, 0, new Label("Contest:"));
		leftFormTable.setWidget(3, 0, new Label(""));
		
		leftFormTable.setText(0, 1, StringHelper.limitedLengthString(botHome.getName(), 20));
		leftFormTable.setText(1, 1, StringHelper.limitedLengthString(botHome.getAuthorName(), 20));
		leftFormTable.setText(2, 1, StringHelper.limitedLengthString(botHome.getContestName(), 20));
		leftFormTable.setText(3, 1, "");
		
		Panel leftTmp = new VerticalPanel();
		leftTmp.setStyleName("botHomeFormPanel");
		leftTmp.add(leftFormTable);
		
		Panel leftPanel = new VerticalPanel();
		leftPanel.add(leftTitle);
		leftPanel.add(leftTmp);
		
		Label rightTitle = new Label("Statistics:");
		rightTitle.setStyleName("title5");
		
		FlexTable rightFormTable = new FlexTable();
		rightFormTable.addStyleName("botHomeFormTable");
		
		rightFormTable.setWidget(0, 0, new Label("Rank:"));
		rightFormTable.setWidget(1, 0, new Label("Wins:"));
		rightFormTable.setWidget(2, 0, new Label("Losses:"));
		rightFormTable.setWidget(3, 0, new Label("Draws:"));
		
		rightFormTable.setText(0, 1, Integer.toString(botHome.getPoints()));
		rightFormTable.setText(1, 1, Integer.toString(botHome.getWins()));
		rightFormTable.setText(2, 1, Integer.toString(botHome.getLosses()));
		rightFormTable.setText(3, 1, Integer.toString(botHome.getDraws()));
		
		Panel rightTmp = new VerticalPanel();
		rightTmp.setStyleName("botHomeFormPanel");
		rightTmp.add(rightFormTable);
		
		Panel rightPanel = new VerticalPanel();
		rightPanel.add(rightTitle);
		rightPanel.add(rightTmp);
		
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(leftPanel);
		panel.add(rightPanel);
		
		panel.setCellHorizontalAlignment(leftPanel, ALIGN_LEFT);
		panel.setCellHorizontalAlignment(rightPanel, ALIGN_LEFT);
		
		add(panel);
		add(showFights);
		setCellHorizontalAlignment(showFights, HorizontalPanel.ALIGN_RIGHT);
	}
	
	private void initShowFightsButton() {
		showFights = new Button(SHOW_FIGHTS_BUTTON_LABEL, new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				StringBuffer buf = new StringBuffer("http://");
                buf.append(Location.getHost()).append("#").append(
                                SiteTokens.BATTLES).append('?')
                                .append(botKey);
                Location.replace(buf.toString());
			}
		});
		showFights.setWidth(SHOW_BUTTONS_WIDTH);
	}
}
